/**
 * Utilities.java
 * @author: liuex
 * 2007-12-25 ����02:29:04
 *
 * note: 
 */
package org.lex.explorer;

import java.io.File;

import org.lex.utils.Pair;
import org.lex.utils.text.SizeFormat;

public class StatisticsUtilities {
	public static final File[] EMPTY = new File[0];

	public static Pair<String, String> generateFileStatistics(File directory) {
		File[] files = directory.listFiles();
		if (null == files)
			files = EMPTY;
		Pair<Integer, Long> p = StatisticsUtilities.getFileStatistics(files);
		StringBuilder buf = new StringBuilder();
		buf.append(files.length).append(" objects ( ");
		buf.append(p.first()).append(" files, ");
		buf.append(files.length - p.first()).append(" folders, ");
		buf.append(SizeFormat.autoFormat(directory.getFreeSpace()));
		buf.append("B free )");
		String info = buf.toString();
		buf.setLength(0);
		buf.append(SizeFormat.autoFormat(p.second())).append('B');
		return new Pair<String, String>(info, buf.toString());
	}

	/**
	 * @param files
	 * @return (file count, total file size)
	 */
	public static Pair<Integer, Long> getFileStatistics(File[] files) {
		if (null == files)
			files = EMPTY;
		int fileCount = 0;
		long size = 0L;
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			if (file.isFile()) {
				fileCount++;
				size += file.length();
			}
		}
		return new Pair<Integer, Long>(fileCount, size);
	}
}
